# -*- coding: utf-8 -*-
"""
@File    : compare_FrameRaw10.py
@Author  : LY
@Time    : 2021/5/13 22:40
"""
import os
from FFT_Interpolation import FFT_interpolation_2
import matplotlib.pyplot as plt
from scipy import signal
import numpy as np
from tqdm import tqdm

file_name = r'C:\Users\admin\Desktop\Resolution\Hor\7.npy'
fs_cam = 50  # Hardware Trigger
pix_size = 5.3e-6
Lamda = 632.8e-9

f = open(file_name, 'rb')
f_size = os.fstat(f.fileno()).st_size
frames = []
time_sequence = []
while f.tell() < f_size:
    frame = np.load(f, allow_pickle=True)
    frames.append(frame[0])
    time_sequence.append(frame[1])

frames = np.array(frames)
if len(frames.shape) == 4:
    print('file_size: %.1fMB' % (f_size / 1e6))
    print('%i frames' % len(frames))
    print('%i line/frame' % len(frames[0]))
    print('%i pixel/line' % len(frames[0][0]))
    print('frame rate: %.2f' % fs_cam)
    print('record time: %f s' % (len(frames) / fs_cam))


img_set = frames
frame_num = len(frames)
window = signal.gaussian(1000, std=1000/8)

''' 
    For single frame
'''
img = img_set[0]
hor_freqs = []

for line in img[262:762]:
    line = np.array([i[0]+256*i[1]-6 for i in line])
    line = line[:1000] * window

    freq_estim_2, phase_estim_2, freqline, sig_magnitude, sig_phase, m_k_num, X_m_k, freq_for_phase = FFT_interpolation_2(
        sig=line, tau0=pix_size, zero_num=1000 * 99, DC_num=800)
    print(freq_estim_2)
    hor_freqs.append(freq_estim_2)
hor_freqs = [Lamda*f/2*1e6 for f in hor_freqs]
plt.plot(hor_freqs, '.-')
plt.show()

''' 
    For Frame Set
'''
# freq_result = []
# for img in tqdm(img_set):
#     hor_freqs = []
#     for line in img[512-50:512+50]:
#         # line = img[512]
#         line = np.array([i[0]+256*i[1]-25 for i in line])
#         line = line[:1000] * window
#         freq_estim_2, phase_estim_2, freqline, sig_magnitude, sig_phase, m_k_num, X_m_k, freq_for_phase = FFT_interpolation_2(
#             sig=line, tau0=pix_size, zero_num=1000 * 99, DC_num=800)
#         hor_freqs.append(freq_estim_2)
#     hor_freq = np.mean(hor_freqs)
#     freq_result.append(hor_freq)
#
# angle_result = [Lamda*f/2*1e6 for f in freq_result]
# plt.plot(angle_result, '.-')
# plt.show()
